package com.toremo.frontline.rest;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.toremo.frontline.dto.ProjectDTO;
import com.toremo.frontline.services.ProjectServices;

/**
 * A REST adapter for the {@link ProjectServices} component.
 * @author Alessandro Santini
 */
@Controller(value = "projectResource")
@RequestMapping(value="/project")
public class ProjectResource {

	private ProjectServices projectServices;

	@Autowired @Required
	public void setProjectServices(ProjectServices projectServices) {
		this.projectServices = projectServices;
	}

	@RequestMapping(method=RequestMethod.POST)
	public void create(@RequestBody ProjectDTO project) {
		projectServices.create(project);
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public @ResponseBody ProjectDTO find(@PathVariable(value="id") long id) {
		return projectServices.find(id);
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public @ResponseBody List<ProjectDTO> find(
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "description", required = false) String description,
		@RequestParam(value = "startFrom", required = false) Date startFrom,
		@RequestParam(value = "startTo", required = false) Date startTo) {
		return projectServices.find(name, description, startFrom, startTo);
	}
	
	@RequestMapping(value="/search", method=RequestMethod.GET)
	public ModelAndView find() {
		ModelAndView response = new ModelAndView("projects");
		return response;
	}
}
